package carTest;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 14:29:28 2018/10/31
 * @version:2.0
 */
public class Html {
    public static HttpClient client = HttpClients.createDefault();

    public JSONObject postJsonHtml(String url, JSONObject jsonParam) {
        HttpPost httpPost = new HttpPost(url);
        // 设置请求的header
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String json2 = null;
        try {
            json2 = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(json2);

        // 打印执行结果
        return jsonObject;
    }

    public String getHtml(String url) {
        HttpGet httpGet = new HttpGet(url);
        //httpGet.setConfig(requestConfig);
        //执行 get请求
        HttpResponse response = null;
        try {
            response = client.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(url + "获取网页请求时出错" + e);
        }
        try {
            //返回获取实体
            HttpEntity entity = response.getEntity();
            //获取网页内容，指定编码

            String web = EntityUtils.toString(entity, "UTF-8");
            return web;
        } catch (IOException e) {
            System.out.println(url + "获取网页响应时出错" + e);
        }
        return null;
    }

    public String postHtml(String url, List<NameValuePair> list) {
        HttpPost httpPost = new HttpPost(url);
        UrlEncodedFormEntity uefEntity = null;
        try {
            uefEntity = new UrlEncodedFormEntity(list, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("封装参数出错");
        }
        httpPost.setEntity(uefEntity);
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(url + "获取网页请求时出错" + e);
        }
        try {
            HttpEntity entity = response.getEntity();
            String web = EntityUtils.toString(entity, "UTF-8");
            return web;
        } catch (IOException e) {
            System.out.println(url + "获取网页响应时出错" + e);
        }
        return null;
    }
}
